/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.vegetablefield.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.modules.person.entity.PersonFarmmanagement;
import com.jeesite.modules.person.service.PersonFarmmanagementService;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.vegetablefield.entity.Vegetablefield;
import com.jeesite.modules.vegetablefield.service.VegetablefieldService;

import java.util.UUID;

/**
 * js_vegetablefieldController
 * @author 达芬奇
 * @version 2019-10-05
 */
@Controller
@RequestMapping(value = "${adminPath}/vegetablefield/vegetablefield")
public class VegetablefieldController extends BaseController {

	@Autowired
	private VegetablefieldService vegetablefieldService;


	@Autowired
	private PersonFarmmanagementService personFarmmanagementService;


	private String flag="";
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public Vegetablefield get(String id, boolean isNewRecord) {
		return vegetablefieldService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("vegetablefield:vegetablefield:view")
	@RequestMapping(value = {"list", ""})
	public String list(Vegetablefield vegetablefield, Model model) {
		model.addAttribute("vegetablefield", vegetablefield);
		return "modules/vegetablefield/vegetablefieldList";
	}

	/**
	 * 可以购买菜地列表
	 */
	@RequiresPermissions("vegetablefield:vegetablefield:view")
	@RequestMapping(value = {"Buylist", ""})
	public String Buylist(Vegetablefield vegetablefield, Model model) {
		model.addAttribute("vegetablefield", vegetablefield);
		return "modules/vegetablefield/vegetablefieldBuyList";
	}


	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("vegetablefield:vegetablefield:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<Vegetablefield> listData(Vegetablefield vegetablefield, HttpServletRequest request, HttpServletResponse response) {
		vegetablefield.setPage(new Page<>(request, response));
		Page<Vegetablefield> page = vegetablefieldService.findPage(vegetablefield);
		return page;
	}


	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("vegetablefield:vegetablefield:view")
	@RequestMapping(value = "form")
	public String form(Vegetablefield vegetablefield, Model model) {
		model.addAttribute("vegetablefield", vegetablefield);
		return "modules/vegetablefield/vegetablefieldForm";
	}

	/**
	 * 购买需要填写的表单
	 */
	@RequiresPermissions("vegetablefield:vegetablefield:view")
	@RequestMapping(value = "Buyformedit")
	public String Buyformedit(Vegetablefield vegetablefield, Model model) {
		model.addAttribute("vegetablefield", vegetablefield);
		flag="Buyformedit";
		return "modules/vegetablefield/vegetablefieldBuyForm";
	}

	/**
	 * 保存菜地管理
	 */
	@RequiresPermissions("vegetablefield:vegetablefield:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated Vegetablefield vegetablefield) {
		if(flag.equals("Buyformedit")){
			//更新菜地当前面积 剩余面积减去购买面积
           Vegetablefield vegetablefield1= vegetablefieldService.get(vegetablefield.getId());
           vegetablefield1.setArea(vegetablefield1.getArea()-vegetablefield.getArea());
           vegetablefieldService.save(vegetablefield1);
			//将购买信息写入
			PersonFarmmanagement personFarmmanagement = new PersonFarmmanagement();
			personFarmmanagement.setFsize(vegetablefield.getArea());
			User u = UserUtils.getUser();
			personFarmmanagement.setUid(u.getUserCode());
			personFarmmanagement.setFid(vegetablefield.getId());
			personFarmmanagementService.save(personFarmmanagement);
		//	personFarmmanagement.set

			//	personFarmmanagement.setUid();
			return renderResult(Global.TRUE, text("购买菜地成功！"));
		}
		vegetablefieldService.save(vegetablefield);
		return renderResult(Global.TRUE, text("保存菜地管理成功！"));
	}
	
	/**
	 * 删除菜地管理
	 */
	@RequiresPermissions("vegetablefield:vegetablefield:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(Vegetablefield vegetablefield) {
		vegetablefieldService.delete(vegetablefield);
		return renderResult(Global.TRUE, text("删除菜地管理成功！"));
	}

	/**
	 * 跳转地图
	 */
	/**
	 * 购买需要填写的表单
	 */
	@RequiresPermissions("vegetablefield:vegetablefield:view")
	@RequestMapping(value = "visitmap")
	public String visitmap(Model model) {
		return "modules/vegetablefield/vegetablefieldMap";
	}

	
}